﻿using System.Collections.Generic;
using Felbot.IO;

namespace Felbot.Interaction {
    /// <summary>
    /// This static class allows you to manage commands.
    /// </summary>
    public static class CommandControl {
        private static XmlControl<XmlCommandRoot> m_XmlControl;

        private static List<XmlCommand> m_CommandList;

        private static List<XmlFlag> m_FlagsList;

        /// <summary>
        /// Gets or sets a list of the command objects currently loaded.
        /// </summary>
        public static List<XmlCommand> CommandList {
            get {
                return m_CommandList;
            }
            set {
                m_CommandList = value;
            }
        }

        /// <summary>
        /// Gets or sets a list of the flag objects currently loaded.
        /// </summary>
        public static List<XmlFlag> FlagsList {
            get {
                return m_FlagsList;
            }
            set {
                m_FlagsList = value;
            }
        }

        /// <summary>
        /// Initializes the commands. Loads them from the file.
        /// </summary>
        public static void Initialize() {
            m_XmlControl = new XmlControl<XmlCommandRoot>(Program.ApplicationFolder + "\\commands.fbx");
            m_CommandList = m_XmlControl.Document.Commands;
            m_FlagsList = m_XmlControl.Document.Flags;
        }

        /// <summary>
        /// Saves the Commands.fbx.
        /// </summary>
        public static void Save() {
            m_XmlControl.Document.Commands = CommandList;
            m_XmlControl.Document.Flags = FlagsList;
            m_XmlControl.Save();
        }

        /// <summary>
        /// Reloads the Commands.fbx.
        /// </summary>
        public static void Reload() {
            Initialize();
        }

        /// <summary>
        /// Gets a command by command name.
        /// </summary>
        /// <param name="name">The command name.</param>
        /// <returns>An XmlCommand object containing command information.</returns>
        public static XmlCommand GetEntry(string name) {
            foreach (XmlCommand command in m_CommandList)
                if (command.Name == name)
                    return command;
            return null;
        }

        /// <summary>
        /// Gets a string representing a free "NewCommand#" command to make a new command by the name of.
        /// </summary>
        /// <returns>Returns a string representing a number.</returns>
        public static string GetMakeName {
            get {
                int l = 1;
                while (GetEntry("NewCommand" + l.ToString()) != null) {
                    l++;
                }
                return "NewCommand" + l.ToString();
            }
        }

        /// <summary>
        /// Creates a new command by the specified command.
        /// </summary>
        /// <param name="name">The name of the command.</param>
        /// <returns>The command created.</returns>
        public static XmlCommand MakeCommand(string name) {
            XmlCommand command = new XmlCommand();
            command.Name = name;
            command.Function = "ScriptName.functionName";
            command.Requires = new XmlCmdRequires();
            command.Requires.Flags = new List<string>();
            command.Aliases = new List<string>();
            command.Description = "";
            m_CommandList.Add(command);
            Save();
            return command;
        }

        /// <summary>
        /// Removes the specified command from the list.
        /// </summary>
        /// <param name="entry">The command object.</param>
        public static void RemoveEntry(XmlCommand command) {
            m_CommandList.Remove(command);
            Save();
        }
    }
}
